package cn.t12.quartzspringbootdemo.config;

import cn.t12.quartzspringbootdemo.job.RemindJob;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import java.util.Date;

@Configuration
public class QuartzConfig {
    // 任务详情
    @Bean
    public JobDetailFactoryBean jobDetail(){
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        // 指定要执行的具体任务
        jobDetailFactoryBean.setJobClass(RemindJob.class);
        jobDetailFactoryBean.setGroup("remindGroup");
        jobDetailFactoryBean.setName("remindJob");
        // 可以被持久化
        jobDetailFactoryBean.setDurability(true);
        return jobDetailFactoryBean;
    }

    // 触发器
    // @Bean
    // public SimpleTriggerFactoryBean simpleTrigger(){
    //     SimpleTriggerFactoryBean simpleTrigger = new SimpleTriggerFactoryBean();
    //     // 关联要执行的任务
    //     simpleTrigger.setJobDetail(jobDetail().getObject());
    //     simpleTrigger.setStartTime(new Date(System.currentTimeMillis()+2000));
    //     simpleTrigger.setRepeatCount(5);
    //     simpleTrigger.setRepeatInterval(5000);
    //     return simpleTrigger;
    // }

    @Bean
    public CronTriggerFactoryBean cronTrigger(){
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        // 关联要执行的任务
        trigger.setJobDetail(jobDetail().getObject());
        trigger.setStartTime(new Date(System.currentTimeMillis()+2000));
        trigger.setCronExpression("0/5 * * ? * 3 2020");
        // 15 15:28   -> 15:43  //15:40 15:55
        // 延时队列,消息中间件实现超时订单的处理
        //trigger.setCronExpression("0/5 * * * * ? 2020");
        return trigger;
    }
}
